
#include "trainNetworkWizard.h"
#include "imageLoader.h"
#include "CNetworkManager.h"
#include "networkRunner.h"

#include <QtCore/QString>
#include <QtCore/QDir>
#include <QtGui/QFileDialog>

/*----------------------------------------------------------------------------*/


TrainNetworkWizard::TrainNetworkWizard( ImageLoader& _imageLoader, CNetworkManager& _networkManager )
	:	m_ui( new Ui::TrainNetworkWizardUi )
	,	m_imageLoader( _imageLoader )
	,	m_networkManager( _networkManager )
	,	m_networkRunner( NULL )
{
	m_ui->setupUi( this );

	/** Button: train - start training */
	connect( m_ui->m_trainButton, SIGNAL( clicked() ), this, SLOT( StartTraining() ) );

	/** Button: stop - terminate training thread */
	connect( m_ui->m_stopButton, SIGNAL( clicked() ) , &m_networkManager, SLOT( Terminate() ) );		

	/** Check box: image location */
	connect( m_ui->m_locationCheckBox, SIGNAL( stateChanged( int ) ), this, SLOT( LocationEnabled( int ) ) );

	/** Button: select location */
	connect( m_ui->m_locationButton, SIGNAL( clicked() ), this, SLOT( SelectLocation() ) );

	/** Connect network trainer with console */
	connect( &m_networkManager, SIGNAL( Progress( const QString& ) ), m_ui->m_console, SLOT ( appendPlainText( const QString& ) ) );
	
	/** Set default path */
	m_ui->m_imagesLocation->setText( QDir::current().absolutePath() + "/" );	
	
	/** Stop button initially invisible */
	m_ui->m_stopButton->hide();
}


/*----------------------------------------------------------------------------*/


TrainNetworkWizard::~TrainNetworkWizard()
{	
	if( m_networkRunner )
		delete m_networkRunner;
	if( m_ui )
		delete m_ui;	
}


/*----------------------------------------------------------------------------*/


void TrainNetworkWizard::ShowCentered( QWidget& _parent )
{
	/** move child window to the parent's center */
	this->move( _parent.frameGeometry().center() - this->rect().center() );
	this->show();
}


/*----------------------------------------------------------------------------*/


void TrainNetworkWizard::StartTraining()
{	
	/** load images */
	m_imageLoader.LoadBmpFilesToImages( m_ui->m_imagesLocation->text() );

	/** create network runner */
	m_networkRunner = new NetworkRunner(
			m_networkManager
		,	m_imageLoader.GetImages()
		,	m_ui->m_speedSpinBox->value()
		,	m_ui->m_errorSpinBox->value()		
	);	

	/** connect training finished signal with deleting network runner */
	connect( m_networkRunner, SIGNAL( finished() ), this, SLOT ( TrainingFinished() ) );
	
	/** set proper train/stop buttons state */
	TrainingState( true );

	/** start train thread */
	m_networkRunner->start();	
}


/*----------------------------------------------------------------------------*/


void TrainNetworkWizard::SelectLocation()
{	
	QString trainDir = QFileDialog::getExistingDirectory(
			this
		,	tr("Open Directory")
		,	QDir::current().absolutePath()
		,	QFileDialog::ShowDirsOnly
	);	

	if ( !trainDir.size() )
		return;
	
	m_ui->m_imagesLocation->setText( trainDir + "/" );
}


/*----------------------------------------------------------------------------*/


void TrainNetworkWizard::LocationEnabled( int _state )
{
	m_ui->m_imagesLocation->setEnabled( !_state );
	m_ui->m_locationButton->setEnabled( !_state );
}


/*----------------------------------------------------------------------------*/


void TrainNetworkWizard::TrainingFinished()
{
	TrainingState( false );
	if ( m_networkRunner )
		delete m_networkRunner;
	m_networkRunner = NULL;
}


/*----------------------------------------------------------------------------*/


void TrainNetworkWizard::TrainingState( bool _state )
{
	if( _state )
	{
		m_ui->m_stopButton->show();
		m_ui->m_trainButton->hide();
	}
	else
	{
		m_ui->m_stopButton->hide();
		m_ui->m_trainButton->show();
	}	
}


/*----------------------------------------------------------------------------*/

